package com.example.docking.service.impl;

import com.example.docking.dao.jindie.GenericDataItem;
import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.response.DepartmentData;
import com.example.docking.service.DepartmentService;
import com.example.docking.util.Constants;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DepartmentServiceImpl implements DepartmentService {
    @Override
    public String getDepartId(DataServiceImpl service, String DepartmentName) throws Exception {
        Gson gson = new Gson();
        // 设置分页参数
        int page = 1; // 初始页码
        int pageSize = 10; // 每页记录数
        HashMap<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        map.put("page_size", String.valueOf(pageSize));
        String depart_id = "";

        ApiResult DataResult = service.SelectDataDetail(Constants.DEPARTMENT, map, "");
        Type DepartmentDataType = new TypeToken<ResponseData<GenericDataItem<DepartmentData>>>() {
        }.getType();
        ResponseData<GenericDataItem<DepartmentData>> responseDepartmentData = gson.fromJson(DataResult.getBody(), DepartmentDataType);
        if (responseDepartmentData.getErrcode() == 0) {
            GenericDataItem<DepartmentData> genericDataItemDepart = responseDepartmentData.getData();

            // 解析分页信息
            String count = genericDataItemDepart.getCount();
            int countNum = Integer.valueOf(count);
            int Pagesum = pageSize;
            List<DepartmentData> DepartData = genericDataItemDepart.getRows();

            List<DepartmentData> allDepartData = new ArrayList<>(DepartData);
            // 如果不是最后一页，继续获取下一页数据
            while (Pagesum < countNum) {
                page++;
                map.put("page", String.valueOf(page));
                DataResult = service.SelectDataDetail(Constants.DEPARTMENT, map, "");
                responseDepartmentData = gson.fromJson(DataResult.getBody(), DepartmentDataType);
                if (responseDepartmentData.getErrcode() == 0) {
                    genericDataItemDepart = responseDepartmentData.getData();
                    DepartData = genericDataItemDepart.getRows();
                    allDepartData.addAll(DepartData);
                    Pagesum = pageSize * page;
                } else {
                    throw new RuntimeException(responseDepartmentData.getErrcode() + ":" + responseDepartmentData.getDescription());
                }
            }
            depart_id = allDepartData.get(0).getId();
//        boolean departmentFound = false;
//        for (DepartmentData departmentData : allDepartData) {
//            if (departmentData.getName().equals(DepartmentName)) {
//                depart_id = departmentData.getId();
//                departmentFound = true;
//                break;
//            }
//        }
//        if (!departmentFound) {
//            // 这里可以根据业务需求添加未找到部门时的处理逻辑，比如抛异常等
//            // 目前简单返回空字符串
//            depart_id = "";
//        }
        } else {
            throw new RuntimeException(responseDepartmentData.getErrcode() + ":" + responseDepartmentData.getDescription());
        }

        return depart_id;
    }
}